package com.example.commoditymanagementsystemspringboot.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.commoditymanagementsystemspringboot.dao.GoodsDao;
import com.example.commoditymanagementsystemspringboot.po.Goods;
import com.example.commoditymanagementsystemspringboot.po.PageBean;
import com.example.commoditymanagementsystemspringboot.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 雷族
 */
@Service()
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDao goodsDao;

    /**
     * 添加商品
     *
     * @param goods the goods
     * @return the int
     * @throws Exception the exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(Goods goods) throws Exception {
        if (goods.getUserId() != null) {
            return goodsDao.insert(goods);
        }
        throw new Exception("添加商品用户id不能为空！");
    }

    /**
     * 模糊查询商品集合
     *
     * @param goods the goods
     * @return the list
     */
    @Override
    public List<Goods> findAll(Goods goods) {
        return goodsDao.findAll(goods);
    }

    /**
     * 通过用户id查找该用户全部商品数据
     *
     * @param userId the userId
     * @return the list
     */
    @Override
    public List<Goods> findAll(Integer userId) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        return goodsDao.selectList(wrapper);
    }

    /**
     * 修改用户记录
     *
     * @param goods the goods
     * @return the int
     * @throws Exception the exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(Goods goods) throws Exception {
        if (goods.getId() != null) {
            UpdateWrapper<Goods> wrapper = new UpdateWrapper<>();
            wrapper.eq("id", goods.getId());
            goods.setId(null);
            return goodsDao.update(goods, wrapper);
        }
        throw new Exception("条件id不能为空！");
    }

    /**
     * 删除用户
     *
     * @param id the id
     * @return the int
     * @throws Exception the exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Integer id) throws Exception {
        return goodsDao.deleteById(id);
    }

    /**
     * Find by page page bean.
     *
     * @param goods    the goods
     * @param pageCode the page code
     * @param pageSize the page size
     * @return the page bean
     */
    @Override
    public PageBean findByPage(Goods goods, int pageCode, int pageSize) {
        //查询所有数据
        List<Goods> goodsList = goodsDao.findAll(goods);
        List<Goods> goodsList2 = new ArrayList<>();
        int index = pageSize * (pageCode - 1);
        //当前数据不支持所选择分页直接显示第一页数据
        if (index >= goodsList.size()) {
            for (int i = 0; i < pageSize && i < goodsList.size(); i++) {
                goodsList2.add(goodsList.get(i));
            }
            return new PageBean(goodsList.size(), goodsList2);
        }
        //分页展示数据
        for (int i = 0; i < pageSize && index + i < goodsList.size(); i++) {
            goodsList2.add(goodsList.get(index + i));
        }
        return new PageBean(goodsList.size(), goodsList2);
    }
}
